package com.southminority.ethnic.controller.admin.research;

import com.southminority.ethnic.common.result.PageResult;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.pojo.Paper;
import com.southminority.ethnic.service.research.PaperService;
import com.southminority.ethnic.controller.research.vo.PaperVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学术论文管理后台控制器
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "17 - 学术论文管理API", description = "【后台】科研成果-学术论文的增删改查接口")
@RestController
@RequestMapping("/admin/research/paper")
@CrossOrigin
public class PaperAdminController {

    private final PaperService paperService;

    public PaperAdminController(PaperService paperService) {
        this.paperService = paperService;
    }

    @GetMapping("/page")
    @Operation(summary = "分页和条件查询学术论文", description = "后台管理使用的论文列表接口，支持多种条件筛选和分页。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "查询成功")})
    public R<PageResult<PaperVO>> getPaperPage(
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页显示数量", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "关键词（标题、作者）", example = "语音识别") @RequestParam(required = false) String keyword,
            @Parameter(description = "论文类型", example = "期刊论文") @RequestParam(required = false) String paperType,
            @Parameter(description = "状态", example = "1", schema = @Schema(type = "integer", allowableValues = {"1", "0"})) @RequestParam(required = false) Integer status) {
        return R.ok(paperService.getPaperPageForAdmin(page, size, keyword, paperType, status));
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取单个学术论文的详细信息（用于编辑）", description = "根据论文ID查询，返回其完整信息。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "论文不存在")
    })
    public R<PaperVO> getPaperDetail(@Parameter(description = "论文的唯一ID", example = "1", required = true) @PathVariable Long id) {
        return R.ok(paperService.getPaperDetailForAdmin(id));
    }

    @PostMapping("/create")
    @Operation(summary = "新增一篇学术论文", description = "传入论文的完整信息（除ID外）来创建一个新的记录。",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "论文信息的JSON对象", required = true,
                    content = @Content(schema = @Schema(implementation = Paper.class))))
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "创建成功")})
    public R<PaperVO> createPaper(@RequestBody Paper paper) {
        log.info("管理员创建论文: {}", paper.getTitle());
        return R.ok(paperService.createPaper(paper));
    }

    @PutMapping("/update/{id}")
    @Operation(summary = "根据ID更新学术论文信息", description = "提供论文ID和更新后的信息，对现有记录进行修改。",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "包含更新字段的论文JSON对象", required = true,
                    content = @Content(schema = @Schema(implementation = Paper.class))))
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "更新成功"), @ApiResponse(responseCode = "404", description = "论文不存在")})
    public R<PaperVO> updatePaper(
            @Parameter(description = "要更新的论文ID", example = "1", required = true) @PathVariable Long id,
            @RequestBody Paper paper) {
        log.info("管理员更新论文: ID={}, Title={}", id, paper.getTitle());
        return R.ok(paperService.updatePaper(id, paper));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "根据ID逻辑删除学术论文", description = "将指定ID的论文记录状态设置为-1（已删除）。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "删除成功")})
    public R<Boolean> deletePaper(@Parameter(description = "要删除的论文ID", example = "1", required = true) @PathVariable Long id) {
        log.info("管理员删除论文: ID={}", id);
        return R.ok(paperService.deletePaper(id));
    }

    @DeleteMapping("/batch-delete")
    @Operation(summary = "批量逻辑删除学术论文", description = "传入一个ID列表，批量将对应的论文记录状态设置为-1。",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "包含多个论文ID的数组", required = true,
                    content = @Content(schema = @Schema(type = "array", implementation = Long.class))))
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "批量删除成功")})
    public R<Boolean> batchDeletePapers(
            @RequestBody List<Long> ids) {
        log.info("管理员批量删除论文: IDs={}", ids);
        return R.ok(paperService.batchDeletePapers(ids));
    }

    @PutMapping("/status/{id}")
    @Operation(summary = "更新学术论文状态", description = "更新单篇论文的状态，如发布（1）和草稿（0）。")
    @Parameters({
            @Parameter(name = "id", description = "论文ID", example = "1", required = true),
            @Parameter(name = "status", description = "新的状态值", example = "0", required = true,
                    schema = @Schema(type = "integer", allowableValues = {"1", "0"}))
    })
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "状态更新成功")})
    public R<Boolean> updatePaperStatus(@PathVariable Long id, @RequestParam Integer status) {
        log.info("管理员更新论文状态: ID={}, Status={}", id, status);
        return R.ok(paperService.updatePaperStatus(id, status));
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口连通性")
    public R<String> test() {
        return R.ok("学术论文管理后台服务正常");
    }
} 
